package com.xx;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;

import java.util.Collections;

/**
 * @author xiaxing
 */
public class CodeGenerator {

    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator(dataSourceConfig());
        generator
                .global(globalConfig())
                .packageInfo(packageConfig())
                .strategy(strategyConfig()).execute();
    }

    private static DataSourceConfig dataSourceConfig() {
        return new DataSourceConfig
                .Builder("jdbc:mysql://127.0.0.1:13316/permission?serverTimezone=GMT%2b8&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false","root","root")
                .dbQuery(new MySqlQuery())
                .build();
    }

    /**
     * 全局配置
     *
     * @return GlobalConfig
     */
    private static GlobalConfig globalConfig() {
        return new GlobalConfig.Builder()
                // 指定输出目录
                .outputDir(System.getProperty("user.dir") + "\\src\\main\\java")
                // 作者名
                .author("aqi")
                // 开启 swagger 模式
//                .enableSwagger()
                // 禁止打开目录
                .disableOpenDir()
                .build();
    }

    /**
     * 包配置
     *
     * @return PackageConfig
     */
    private static PackageConfig packageConfig() {
        return new PackageConfig.Builder()
                .parent("com.xx.permission")
//                .moduleName("demo")
                .entity("entity")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper.xml")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"))
                .build();
    }

    /**
     * 策略配置
     *
     * @return StrategyConfig
     */
    private static StrategyConfig strategyConfig() {
        return new StrategyConfig.Builder()
                .disableSqlFilter()
                // entity文件策略
                .entityBuilder()
                // 开启 lombok 模型
                .enableLombok()
                // 添加父类公共字段(这些字段将不在实体中生成)
                .addSuperEntityColumns("create_time", "update_time")
                // 全局主键类型
                .idType(IdType.AUTO)
                // 添加表字段填充
//                .addTableFills(new Column("create_time", FieldFill.INSERT))
//                .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))

                // 逻辑删除字段名(数据库)
                .logicDeleteColumnName("deleted")

                // controller文件策略
                .controllerBuilder()
                .enableRestStyle()

                // service文件策略
                .serviceBuilder()
                .formatServiceFileName("%sService")
                .build();
    }
}
